import axios from 'axios'
import useAdminStore from "@store/modules/admin"
import { ElMessage } from "element-plus"
const http = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 10000, //超时时间
})
http.interceptors.request.use(config => {
  const userAdmin = useAdminStore(),{ adminUser:{token} } = userAdmin
  if (token) {
    config.headers["token"] = token
  }
  return config
})
http.interceptors.response.use(response => {
 if(response.data && response.data?.code != 200){
   resErrMsg(response.data?.msg??'服务器访问异常');
 }
return response.data
},error => {
  resErrMsg('请求后台异常，请联系系统管理员')
  return Promise.reject(error)
})
const resErrMsg = (msg) => {
  ElMessage.closeAll()
  ElMessage({
    type: 'error',
    message: msg,
  })
}
export default http